package com.ruoyi.mall.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.PmsBrandMapper;
import com.ruoyi.mall.domain.PmsBrand;
import com.ruoyi.mall.service.IPmsBrandService;

/**
 * 商品品牌Service业务层处理
 *
 * @author zhanghang
 * @date 2023-10-31
 */
@Service
public class PmsBrandServiceImpl implements IPmsBrandService {
    @Autowired
    private PmsBrandMapper pmsBrandMapper;

    /**
     * 查询商品品牌
     *
     * @param id 商品品牌主键
     * @return 商品品牌
     */
    @Override
    public PmsBrand selectPmsBrandById(String id) {
        return pmsBrandMapper.selectPmsBrandById(id);
    }

    /**
     * 查询商品品牌列表
     *
     * @param pmsBrand 商品品牌
     * @return 商品品牌
     */
    @Override
    public List<PmsBrand> selectPmsBrandList(PmsBrand pmsBrand) {
        return pmsBrandMapper.selectPmsBrandList(pmsBrand);
    }

    /**
     * 新增商品品牌
     *
     * @param pmsBrand 商品品牌
     * @return 结果
     */
    @Override
    public int insertPmsBrand(PmsBrand pmsBrand) {
        //1.获取品牌名称
        String brandName = pmsBrand.getName();
        //2.判断品牌名称是否为空
        if (brandName == null || brandName.equals("")) {
            throw new RuntimeException("品牌名称不能为空");
        }
        //判断品牌拼音是否为空
        if (pmsBrand.getPinyin() == null || pmsBrand.getPinyin().equals("")) {
            throw new RuntimeException("品牌拼音不能为空");
        }
        //3.判断品牌名称是否重复
        if (!pmsBrandMapper.selectPmsBrandList(pmsBrand).isEmpty()) {
            throw new RuntimeException("品牌名称重复");
        }
        //4.设置新增时间和修改时间
        Date date = new Date();
        pmsBrand.setGmtCreate(date);
        pmsBrand.setGmtModified(date);
        return pmsBrandMapper.insertPmsBrand(pmsBrand);
    }

    /**
     * 修改商品品牌
     *
     * @param pmsBrand 商品品牌
     * @return 结果
     */
    @Override
    public int updatePmsBrand(PmsBrand pmsBrand) {
        //1.判断品牌名称是否为空
        if (pmsBrand.getName() == null || pmsBrand.getName().equals("")) {
            throw new RuntimeException("品牌名称不能为空");
        }
        //判断品牌拼音是否为空
        if (pmsBrand.getPinyin() == null || pmsBrand.getPinyin().equals("")) {
            throw new RuntimeException("品牌拼音不能为空");
        }
        //2.判断品牌名称是否存在
        PmsBrand brand = pmsBrandMapper.selectPmsBrandById(pmsBrand.getId());
        if (brand != null) {
            int row = pmsBrandMapper.selectPmsBrandByName(pmsBrand.getName());
            if (row > 0 && !pmsBrand.getName().equals(brand.getName())) {
                throw new RuntimeException("品牌名称已存在");
            }
        }
        //3.设置修改时间
        Date date = new Date();
        pmsBrand.setGmtModified(date);
        return pmsBrandMapper.updatePmsBrand(pmsBrand);
    }

    /**
     * 批量删除商品品牌
     *
     * @param ids 需要删除的商品品牌主键
     * @return 结果
     */
    @Override
    public int deletePmsBrandByIds(String[] ids) {
        if (ids.length == 0) {
            throw new RuntimeException("请选择要删除的品牌");
        }
        return pmsBrandMapper.deletePmsBrandByIds(ids);
    }

    /**
     * 删除商品品牌信息
     *
     * @param id 商品品牌主键
     * @return 结果
     */
    @Override
    public int deletePmsBrandById(String id) {
        return pmsBrandMapper.deletePmsBrandById(id);
    }
}
